package main;

import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

public class SmsSender implements Runnable {

    MessageConnection conn = null;
    TextMessage txtmessage = null;
    String shortCode = "";
    String fullSMS = "";
    private static SmsSender sender;
    public int sentResult;

    public static SmsSender getInstance() {
        if (sender == null) {
            sender = new SmsSender();
        }
        return sender;
    }

    public void sendSms(String shortCode, String fullSMS) {
        this.shortCode = shortCode;
        this.fullSMS = fullSMS;
        Debug.d("code: " + this.shortCode);
        Debug.d("fullSMS: " + this.fullSMS);
        sentResult = -1;
        new Thread(this).start();
    }

    public void run() {
        try {
            conn = (MessageConnection) Connector.open("sms://" + shortCode);
            txtmessage = (TextMessage) conn.newMessage(
                    MessageConnection.TEXT_MESSAGE);
            txtmessage.setPayloadText(fullSMS);
            conn.send(txtmessage);
            sentResult = 1;
        } catch (Exception ex) {
            sentResult = 0;
        } finally {
            try {
                conn.close();
            } catch (Exception ex) {
            }
        }
    }
}
